﻿using Creator.Core.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Creator;
using System.Xml.Linq;
using HandyControl.Interactivity;
using System.Data.Odbc;
using System.Windows.Markup;
using ControlHelper.WPF;
using Creator.Core.Component;
using DesktopCreator.ViewModel;

namespace DesktopCreator.Controls
{
  
    /// <summary>
    /// Inspector.xaml 的交互逻辑
    /// </summary>
    public partial class Inspector : UserControl
    {
        public static Action Clear;
        public Inspector()
        {
            InitializeComponent(); 
            //打开组件菜单 添加组件
            btnAddComponent.Click += (s, e) =>
            {
                if (dlComponent == null) return;
                dlComponent.ToggleUIVisable();
                sp.ToggleUIVisable();
                dlComponent.CreateTpyeName(typeof(IComponent), _object);
            };
            Clear += () =>
            {
                sp.Children.Clear();
                _object = null;
            }; 

        }
        private UCDropList dlComponent;
        public BaseObject _object;
   
        /// <summary>
        ///设置当前检视窗内容
        /// </summary>
        /// <param name="data"></param>
        public void SetObjectData(BaseObjectViewModel bovm)
        {
            var data = bovm.Object;
            if (_object != null && data.Id == _object.Id) return;
            this.DataContext = bovm;
            sp.Children.Clear();
            _object = data;
            btnAddComponent.Visibility = Visibility.Collapsed; 
           
            txtObjectName.IsReadOnly = false;
            cbActive.IsEnabled = true;
            if (data is ProcessObject)
            { 
                if (dlComponent != null) 
                    gridMain.Children.Remove(dlComponent);
                dlComponent = new UCDropList()
                {
                    Visibility = Visibility.Collapsed,
                    VerticalAlignment = VerticalAlignment.Top,
                };
                Grid.SetRow(dlComponent, 0); 
                

                //当创建组件的时
                dlComponent.OnCreate += (dlbh) =>
                {
                    if (dlbh is Behaviour cbh)
                    {
                        data.AddBehaviour(cbh);
                        sp.Children.Add(GetUCComponent(cbh));
                    }
                    sp.ToggleUIVisable();
                };
                 
                if (data.Behaviours == null) return;
                foreach (var behaviour in data.Behaviours)
                {
                    sp.Children.Add(GetUCComponent(behaviour));
                }
                gridMain.Children.Add(dlComponent);
                btnAddComponent.Visibility = Visibility.Visible;
                return;
            } 


            if (data.OtherData == null) return;

            var cc = new ClassControl(data.OtherData.GetType(),true,data.OtherData );
            txtObjectName.IsReadOnly = true;
            cbActive.IsEnabled = false;
            sp.Children.Add(cc); 
        }
         
        /// <summary>
        /// 创建组件UI
        /// </summary>
        /// <param name="behaviour"></param>
        /// <returns></returns>
        private UCComponent GetUCComponent(Behaviour behaviour)
        {
            var bh = new UCComponent(behaviour);
            //从主界面移除时
            bh.OnRemoved += (ebh) =>
            {
                sp.Children.Remove(ebh);
            }; 
            return bh;
        }
       
    }
}
